import tensorflow as tf
import tensorlayer as tl
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
sess = tf.InteractiveSession()

X_train, y_train, X_val, y_val, X_test, y_test = \
                                tl.files.load_mnist_dataset(shape=(-1,784))

# plt.imshow(ddd)
# ddd[ddd>0]=1
# ddd=np.array(ddd)
# print(ddd)
# print(X_test.shape)
# dddd=[x for x in ddd if x > 0]
# print(dddd)


                 
# 定义模型
x = tf.placeholder(tf.float32, shape=[None, 784], name='x')

network = tl.layers.InputLayer(x, name='input_layer')
network = tl.layers.DropoutLayer(network, keep=0.8, name='drop1')
network = tl.layers.DenseLayer(network, n_units=800,
                                act = tf.nn.relu, name='relu1')
network = tl.layers.DropoutLayer(network, keep=0.5, name='drop2')
network = tl.layers.DenseLayer(network, n_units=800,
                                act = tf.nn.relu, name='relu2')
network = tl.layers.DropoutLayer(network, keep=0.5, name='drop3')
network = tl.layers.DenseLayer(network, n_units=10,
                                act = tf.identity,
                                name='output_layer')

load_params = tl.files.load_npz(path='', name='model.npz')
sess.run(tf.initialize_all_variables())
tl.files.assign_params(sess, load_params, network)

print(network)

y = network.outputs
y_op = tf.argmax(tf.nn.softmax(y), 1)


i=0
while i<10000:
    ddd=X_test[i].reshape(1,28*28)
    dddtmp=X_test[i].reshape(28,28)*255
    # plt.plot(dddtmp)
    fig = plt.figure()  
    # 第一个子图,按照默认配置  
    ax = fig.add_subplot(111)  
    ax.imshow(Image.fromarray(dddtmp)) 
    plt.show()


    print(tl.utils.predict(sess, network, ddd, x, y_op))
    i=i+1
sess.close()